package web_package

import org.hibernate.cfg.Configuration
import db.User

object Login {
	def login(email : String, password : String) : User = {
		val cfg = new Configuration()
		var sessionFactory = null.asInstanceOf[org.hibernate.SessionFactory]
		var session_ = null.asInstanceOf[org.hibernate.Session]
	    try{
	    	sessionFactory = cfg.configure().buildSessionFactory()
	    	session_ = sessionFactory.openSession()
	    	val query = session_.createQuery("from User u where u.email = '"
	    							+email+"' and u.password = '"+password+"'")
	    	if(query.list.toArray.isEmpty){
	    	  session_.flush
	    	  return null
	    	}
	    	val usr = query.list.toArray.first.asInstanceOf[User]
	    	session_.flush
	    	return usr
	    }catch{ case(e : Exception) => e.printStackTrace}
	    return null
/*		val user = new User;
		user.setUserID(new java.lang.Long(1))
		user.setEmail(email)
		user.setPassword(password)
		user.setFirstName("Tamar")
		user.setLastName("Samson")
		user
		*/
	  }
}
